﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CopyMsgToTxt.Helper
{
    public class WindowFocusHelper
    {
        // 导入必要的 Windows API 函数
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private const int SW_RESTORE = 9;

        public static void FocusWindow(IntPtr hwnd)
        {
            if (hwnd == IntPtr.Zero)
            {
                throw new ArgumentException("Invalid window handle");
            }

            // 将窗口显示并恢复（如果窗口最小化了）
            ShowWindow(hwnd, SW_RESTORE);

            // 将窗口设置为前台窗口
            if (!SetForegroundWindow(hwnd))
            {
                Console.WriteLine("Failed to set foreground window");
            }
        }
    }
}
